package com.hzf.wxplatform.web.controller.manager.owner;

import com.google.gson.JsonObject;
import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.ByteUtils;
import com.hzf.common.util.RedisUtil;
import com.hzf.config.dao.model.ConfigRegion;
import com.hzf.config.dao.model.ConfigRegionExample;
import com.hzf.ucenter.dao.model.UcenterUser;
import com.hzf.ucenter.dao.model.UcenterUserCity;
import com.hzf.wxplatform.common.utils.UcenterUserUtils;
import net.sf.json.JSONObject;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/ownerIndex")
public class OwnerIndexController extends BaseController {
    @Autowired
    ApiService apiService;
    @RequestMapping("/index")
    private String index(Model model){

        return  thymeleaf("/YZ/YZ_Index");
    }
    @RequestMapping("/getAddress")
    private String getAddress(Model model,HttpServletRequest request){
        byte[] a = UcenterUserUtils.getUser(request.getSession());
        if (null == a) {
            model.addAttribute("data", "您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return thymeleaf("/404");
        }
        UcenterUser ucenterUser = (UcenterUser) ByteUtils.ByteToObject(a);
        if (null == ucenterUser) {
            model.addAttribute("data", "您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return thymeleaf("/404");
        }
        List<UcenterUserCity> cityList = new ArrayList<>();
       Set<String> stringlist = RedisUtil.SMEMBERS("address"+ucenterUser.getId());
        for(String address :stringlist){
            JSONObject jsonobject = JSONObject.fromObject(address);
            UcenterUserCity user= (UcenterUserCity)JSONObject.toBean(jsonobject,UcenterUserCity.class);
            cityList.add(user);
        }
        ConfigRegionExample configRegionExample = new ConfigRegionExample();
        List<ConfigRegion> list = apiService.selectRegion(configRegionExample);
        String address = RedisUtil.get("dangqianweizhi"+ucenterUser.getId());
        JSONObject jsonobject = JSONObject.fromObject(address);
        UcenterUserCity user= (UcenterUserCity)JSONObject.toBean(jsonobject,UcenterUserCity.class);
        model.addAttribute("dataCity",user);
        model.addAttribute("data",list);
        model.addAttribute("cityList",cityList);
        return thymeleaf("/YZ/YZ_CYDZ");
    }
}
